package com.kacheap.config;

import java.util.concurrent.Executors;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.kacheap.event.HelloEventFactory;
import com.kacheap.handler.HelloEventHandler;
import com.kacheap.model.MessageModel;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

@Configuration
public class MQManager {

	@Bean("messageModel")
	public RingBuffer<MessageModel> messageModelRingBuffer() {
		// 指定ringbuffer字节大小，必须为2的N次方（能将求模运算转为位运算提高效率），否则将影响效率
		int bufferSize = 1024 * 256;
				
		// 指定事件工厂
		HelloEventFactory factory = new HelloEventFactory();
		
		Disruptor<MessageModel> disruptor = new Disruptor<>(factory, bufferSize, Executors.defaultThreadFactory());
		

		// 定义用于事件处理的线程池，Disruptor通过java.util.concurrent.ExecutorSerivce提供的线程来触发consumer的事件处理
		// ExecutorService executor = Executors.newFixedThreadPool(1);
		// 单线程模式，获取额外的性能
		// Disruptor<MessageModel> disruptor = new Disruptor<>(factory, bufferSize, executor, ProducerType.SINGLE, new BlockingWaitStrategy());
		
		

		// 设置事件业务处理器---消费者
		disruptor.handleEventsWith(new HelloEventHandler());

		// 启动disruptor线程
		disruptor.start();

		// 获取ringbuffer环，用于接取生产者生产的事件

		return disruptor.getRingBuffer();
	}

}